#/
# @license Apache-2.0
#
# Copyright (c) 2021 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#/

# VARIABLES #

# Define the path to the remark configuration file:
REMARK_RELATED_CONF ?= $(CONFIG_DIR)/remark/.remarkrc.js

# Define the path to the remark ignore file:
# REMARK_RELATED_IGNORE ?= $(CONFIG_DIR)/remark/.remarkignore FIXME
REMARK_RELATED_IGNORE ?= $(ROOT_DIR)/.remarkignore

# Define the path to a plugin which inserts a "See Also" section of related `stdlib` packages:
REMARK_RELATED_PLUGIN ?= $(TOOLS_PKGS_DIR)/remark/plugins/remark-stdlib-related
REMARK_RELATED_PLUGIN_SETTINGS ?=
REMARK_RELATED_PLUGIN_FLAGS ?= --use $(REMARK_RELATED_PLUGIN)=$(REMARK_RELATED_PLUGIN_SETTINGS)

# Define command-line options when invoking the remark executable:
REMARK_RELATED_FLAGS ?= \
	--ext $(MARKDOWN_FILENAME_EXT) \
	--rc-path $(REMARK_RELATED_CONF) \
	--ignore-path $(REMARK_RELATED_IGNORE)

# Define the remark output option:
REMARK_RELATED_OUTPUT_FLAG ?= --output

# Define a directory for writing temporary files:
REMARK_RELATED_TMP_DIR ?= $(TMP_DIR)

# Define a temporary file for writing a list of files to be processed:
REMARK_RELATED_TMP_FILE_LIST ?= $(TMP_DIR)/remark_related_tmp_file_list.txt

# Define a temporary file for writing a list of files which have been processed:
REMARK_RELATED_TMP_FILE_LIST_PROGRESS ?= $(TMP_DIR)/remark_related_tmp_file_list_progress.txt


# RULES #

#/
# Creates a directory for writing temporary files.
#
# @private
#/
$(REMARK_RELATED_TMP_DIR):
	$(QUIET) $(MKDIR_RECURSIVE) $(REMARK_RELATED_TMP_DIR)

#/
# Creates a file containing a list of files to be processed.
#
# @private
#/
$(REMARK_RELATED_TMP_FILE_LIST): | $(REMARK_RELATED_TMP_DIR)
	$(QUIET) $(TOUCH) $(REMARK_RELATED_TMP_FILE_LIST)
	$(QUIET) $(FIND_MARKDOWN_CMD) > $(REMARK_RELATED_TMP_FILE_LIST)

#/
# Creates a file for writing the list of files which have been processed.
#
# @private
#/
$(REMARK_RELATED_TMP_FILE_LIST_PROGRESS): | $(REMARK_RELATED_TMP_DIR)
	$(QUIET) $(TOUCH) $(REMARK_RELATED_TMP_FILE_LIST_PROGRESS)

#/
# Updates the related packages section of Markdown files.
#
# ## Notes
#
# -   This rule is useful when wanting to glob for Markdown files (e.g., process all Markdown files for a particular package).
# -   In order to insert a list of related `stdlib` packages, a Markdown file must contain a "related" section tag (i.e., `<section class="related"></section>`).
#
# @param {string} [MARKDOWN_FILTER] - file path pattern (e.g., `.*/math/base/special/.*`)
# @param {string} [MARKDOWN_PATTERN] - filename pattern (e.g., `*.md`)
#
# @example
# make markdown-related
#
# @example
# make markdown-related MARKDOWN_PATTERN='README.md' MARKDOWN_FILTER='.*/math/base/special/.*'
#/
markdown-related: $(NODE_MODULES) clean-markdown-related markdown-related-init
	$(QUIET) $(FIND_MARKDOWN_CMD) | tee $(REMARK_RELATED_TMP_FILE_LIST) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
		echo ""; \
		echo "Processing file: $$file"; \
		"$(REMARK)" \
			$$file \
			$(REMARK_RELATED_FLAGS) \
			$(REMARK_RELATED_PLUGIN_FLAGS) \
			$(REMARK_RELATED_OUTPUT_FLAG) || exit 1; \
		echo "$$file" >> $(REMARK_RELATED_TMP_FILE_LIST_PROGRESS); \
	done

.PHONY: markdown-related

#/
# Updates the related packages section of a specified list of Markdown files.
#
# ## Notes
#
# -   This rule is useful when wanting to process a list of Markdown files generated by some other command (e.g., a list of changed Markdown files obtained via `git diff`).
# -   In order to insert a list of related `stdlib` packages, a Markdown file must contain a "related" section tag (i.e., `<section class="related"></section>`).
#
# @param {string} FILES - list of files
#
# @example
# make markdown-related-files FILES='/foo/foo.md /foo/bar.md'
#/
markdown-related-files: $(NODE_MODULES)
	$(QUIET) for file in $(FILES); do \
		echo ""; \
		echo "Processing file: $$file"; \
		"$(REMARK)" \
			$$file \
			$(REMARK_RELATED_FLAGS) \
			$(REMARK_RELATED_PLUGIN_FLAGS) \
			$(REMARK_RELATED_OUTPUT_FLAG) || exit 1; \
	done

.PHONY: markdown-related-files

#/
# Resumes processing for updating the related packages section of Markdown files.
#
# ## Notes
#
# -   The environment variables are **only** applicable when processing has not already begun. To apply the environment variables, be sure to first run `make clean-markdown-related`.
#
# @param {string} [MARKDOWN_FILTER] - file path pattern (e.g., `.*/math/base/special/.*`)
# @param {string} [MARKDOWN_PATTERN] - filename pattern (e.g., `*.md`)
#
# @example
# make markdown-related-resume
#/
markdown-related-resume: $(NODE_MODULES) $(REMARK_RELATED_TMP_FILE_LIST) $(REMARK_RELATED_TMP_FILE_LIST_PROGRESS)
	$(QUIET) $(UNIQUE_LINES) $(REMARK_RELATED_TMP_FILE_LIST) $(REMARK_RELATED_TMP_FILE_LIST_PROGRESS) | grep '^[\/]\|^[a-zA-Z]:[/\]' | while read -r file; do \
		echo ""; \
		echo "Processing file: $$file"; \
		"$(REMARK)" \
			$$file \
			$(REMARK_RELATED_FLAGS) \
			$(REMARK_RELATED_PLUGIN_FLAGS) \
			$(REMARK_RELATED_OUTPUT_FLAG) || exit 1; \
		echo "$$file" >> $(REMARK_RELATED_TMP_FILE_LIST_PROGRESS); \
	done

.PHONY: markdown-related-resume

#/
# Performs initialization tasks associated with processing Markdown files.
#
# @example
# make markdown-related-init
#/
markdown-related-init: $(REMARK_RELATED_TMP_DIR) $(REMARK_RELATED_TMP_FILE_LIST_PROGRESS)
	$(QUIET) $(TOUCH) $(REMARK_RELATED_TMP_FILE_LIST)

.PHONY: markdown-related-init

#/
# Runs clean-up tasks associated with processing Markdown files.
#
# @example
# make clean-markdown-related
#/
clean-markdown-related:
	$(QUIET) $(DELETE) $(DELETE_FLAGS) $(REMARK_RELATED_TMP_FILE_LIST_PROGRESS)

.PHONY: clean-markdown-related
